package com.qf.controller;

import com.qf.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author lixu
 */
@Controller
public class ParamController {

    /**
     * 传递参数 : 原始的方式
     */
    @RequestMapping("/param/test1")
    public void test1(HttpServletRequest request, HttpServletResponse response, HttpSession httpSession)
      throws Exception{
        String id = request.getParameter("id");
        httpSession.setAttribute("id",id);
        request.setAttribute("name","张三");

        System.out.println("ParamController=test1:");

        request.getRequestDispatcher("/param.jsp").forward(request,response);

        //response.sendRedirect("/param.jsp");
    }
    /**
     * 传递参数 : 基础类型 8大
     *
     * String age = request.getParameter("age");
     * Integer age = Integer.parseInt(age)
     *
     * String isP = request.getParameter("isP");
     *  isP=0 转换成 false
     *  isP=1 转换成 true
     *
     *  http://localhost:8080/param/test2?age=66&name=xiaoxi&isP=true  四个字符  1代表一个字符
     *
     */
    @RequestMapping("/param/test2")
    public void test2(Integer age,String name,Boolean isP){
        System.out.println("ParamController=test2:age:" + age);
        System.out.println("ParamController=test2:name:" + name);
        System.out.println("ParamController=test2:isP:" + isP);
    }

    /**
     * 实体对象  User对象 Student
     */
    @RequestMapping("/param/test3")
    public void test3(User user){
        System.out.println("ParamController=test2:sex:" + user.getSex());
        System.out.println("ParamController=test2:name:" + user.getName());
        System.out.println("ParamController=test2:birthday:" + user.getBirthday());
    }
    /**
     * 数组接收参数
     */
    @RequestMapping("/param/test4")
    public void test4(String[] hobbys){
        for (String hobby : hobbys) {
            System.out.println(hobby);
        }
    }
    /**
     * Rest风格的开发  入门体验
     *
     * @PathVariable:   path：路径   Variable：参数
     */
    @RequestMapping("/param/test5/{id}/{name}/{age}")
    public void test5(
            @PathVariable Integer id,
            @PathVariable String name,
            @PathVariable(value = "age") Integer a
                      ){

        System.out.println("id:" + id);
        System.out.println("name:" + name);
        System.out.println("age:" + a);
    }


}
